package org.happycomp.radio.impl;

import java.util.Calendar;
import java.util.Date;

import org.happycomp.radio.StopDownloadCondition;

public class SecondsTimeIntervalCondition implements StopDownloadCondition {

	public static final java.util.logging.Logger LOGGER = java.util.logging.Logger
			.getLogger(SecondsTimeIntervalCondition.class.getName());
	
	public static final StopDownloadCondition SECONDS_5 = new SecondsTimeIntervalCondition(5);
	public static final StopDownloadCondition SECONDS_15 = new SecondsTimeIntervalCondition(15);
	public static final StopDownloadCondition SECONDS_30 = new SecondsTimeIntervalCondition(30);
	public static final StopDownloadCondition SECONDS_60 = new SecondsTimeIntervalCondition(60);

	private int seconds;
	private Date startDate;
	private boolean manualStop = false;
	
	public SecondsTimeIntervalCondition() {
		
	}
	
	public SecondsTimeIntervalCondition(int seconds) {
		super();
		this.seconds = seconds;
		this.startDate = new Date();
	}

	@Override
	public void manualStop() {
		LOGGER.info("manualy stopped");
		this.manualStop = true;
	}

	@Override
	public boolean isStopped() {
		if (this.manualStop) {
			LOGGER.info("manual stop");
			return true;
		}
		Date thisDate = new Date();
		Calendar instance = Calendar.getInstance();
		instance.setTime(this.startDate);
		instance.add(Calendar.SECOND, this.seconds);
		Date expected = instance.getTime();
		return thisDate.after(expected);
	}

	@Override
	public Date storeToStopDate(Date startDate) {
		throw new UnsupportedOperationException("still unsupported");
	}

	@Override
	public void initFromStopDate(Date startDate, Date stopDate) {
		throw new UnsupportedOperationException("still unsupported");
	}
}
